<?php
include ("config.php");
$con = mysqli_connect("$host","$name","$password","$dbname");
if (!$con) 
{ 
    die("连接错误: " . mysqli_connect_error()); 
} 

$sql = "CREATE TABLE login (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,username VARCHAR(30) NOT NULL,password VARCHAR(30) NOT NULL,phone      VARCHAR(30) NOT NULL,email VARCHAR(50),reg_date TIMESTAMP
)";



$username = $_POST['username'];

$password = $_POST['password'];

$phone = $_POST['phone'];

$email = $_POST['email'];

if($username == "" || $password == "" || $phone == "" || $email == "")

{

 echo "<script>alert('信息不能为空！重新填写');window.location.href='../reg.html'</script>";

} elseif ((strlen($username) < 3)||(!preg_match('/^\w+$/i', $username))) {

 echo "<script>alert('用户名至少3位且不含非法字符！重新填写');window.location.href='zhuce'</script>";

 //判断用户名长度

}elseif(strlen($password) < 5){

 echo "<script>alert('密码至少5位！重新填写');window.location.href='../reg.html'</script>";

 //判断密码长度

}elseif(!preg_match("/^1[34578]\d{9}$/",  $phone)) {

 echo "<script>alert('手机号格式错误，请检查！');window.location.href='../reg.html'</script>";

 //检测两次输入密码是否相同

} elseif (!preg_match('/^[\w\.]+@\w+\.\w+$/i', $email)) {

 echo "<script>alert('邮箱不合法！重新填写');window.location.href='../reg.html'</script>";

 //判断邮箱格式是否合法

}  elseif(mysqli_fetch_array(mysqli_query($con,"select * from login where username = '$username'"))){

 echo "<script>alert('用户名已存在');window.location.href='../reg.html'</script>";

} else{

 $sql= "insert into login(username, password, phone, email)values('$username','$password','$phone','$email')";

 //插入数据库

 if(!(mysqli_query($con,$sql))){

   echo "<script>alert('数据插入失败');window.location.href='../reg.html'</script>"; 

 }else{

   echo "<script>alert('注册成功！);window.location.href='../login.html'</script>";

 }

}

?>